<?php
/**
 * Created by PhpStorm.
 * User: chenxj
 * Date: 2018/11/28
 * Time: 15:35
 */

namespace app\admin\controller;


class Dictionary extends Common
{
    /**
     * 公共配置
     * @return mixed
     */
    public function index() {
        $requestMap = $this->convertRequestToMap();
        $list = $this->dictionaryService->getDictionaryQuery($requestMap);
        $this->assign('list', $list);
        $this->assign('page', $requestMap['page']);
        return $this->fetch();
    }

    /**
     * 新增页面
     * @return mixed
     */
    public function add() {

        return $this->fetch();
    }

    /**
     *执行新增
     * @return mixed
     */
    public function addPost()
    {
        $param = input('post.');
        $configService = model("Dictionary","service");

        try {

            $result = $configService->saveByAllowField($param);

            if($result === false){
                $this->error($configService->getError());
            }

            cache('db_dictionary_data',null);

            $this->success("配置添加成功",url("index"));

        } catch(\PDOException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 编辑配置页面
     * @return mixed
     */
    public function edit() {

        $id = input('param.id');

        $configService = model("Dictionary","service");

        $info = $configService->findById($id);

        $this->assign('data', $info);

        return $this->fetch();

    }

    /**
     * 编辑配置操作
     * @return array|\think\response\Json
     */
    public function editPost() {

        $param = input('post.');

        $configService = model("Dictionary","service");

        try{

            $result = $configService->updateByAllowFieldAndId($param,$param["id"]);

            if($result === false){
                $this->error($configService->getError());
            }

            cache('db_dictionary_data',null);

            $this->success("编辑成功",url("index"));

        } catch(\PDOException $e) {
            $this->error($e->getMessage());
        }

    }

    /**
     * 删除操作
     * @return array|\think\response\Json
     */
    public function delete() {

        $id = input('param.id');

        $configService = model("Dictionary","service");

        try{

            $result = $configService->deleteById($id);

            if(false === $result){
                $this->error($configService->getError());
            }

            cache('db_dictionary_data',null);

            $this->success("删除成功");

        } catch(\PDOException $e) {
            $this->error($e->getMessage());
        }

    }
}